package com.qianji.petmall.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liang zai
 * @date 2022/4/25 16:24
 * @description
 */
@Configuration
public class PetmallFeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//                System.out.println("RequestInterceptor线程...."+Thread.currentThread().getId());
                if (attributes!=null){
                    HttpServletRequest request = attributes.getRequest();
                    if (request!=null){
                        String cookie = request.getHeader("Cookie");
                        template.header("Cookie",cookie);
                    }

                }

            }
        };
    }
}
